//
//  WDSliderTitleView.m
//  MyDemos
//
//  Created by iMac on 2018/3/7.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDSliderTitleView.h"




@interface WDSliderTitleView()


@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, strong) NSArray *indicatorFrames;
@property (nonatomic, strong) UIStackView *buttonStackView;
@property (nonatomic, strong) UIButton *selectedButton;
//@property (nonatomic, strong) NSArray *buttons;

@end

#define SHEIGHT self.frame.size.height
#define SWIDTH self.frame.size.width
#define DEFAULT_COLOR [UIColor colorWithRed:((float)((0x333333 & 0xFF0000) >> 16)) / 255.0 green:((float)((0x333333 & 0x00FF00) >> 8)) / 255.0 blue:((float)(0x333333 & 0x0000FF)) / 255.0 alpha:1.0]
CGFloat defaultIndicatorHeight = 3;
CGFloat buttonTitleFont = 16;
@implementation WDSliderTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupInit];
    }
    return self;
}


- (void)setupInit {
    CGFloat stackHeight = SHEIGHT - defaultIndicatorHeight;
    self.buttonStackView = [[UIStackView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, stackHeight)];
    self.buttonStackView.axis = UILayoutConstraintAxisHorizontal;
    self.buttonStackView.distribution = UIStackViewDistributionFillEqually;
    self.buttonStackView.spacing = 0;
    self.buttonStackView.alignment = UIStackViewAlignmentFill;
    [self addSubview:self.buttonStackView];
    self.indicatorView = [[UIView alloc] init];
    self.indicatorView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.indicatorView];
}


- (void)setButtonTitles:(NSArray *)buttonTitles {
    _buttonTitles = buttonTitles;
    int buttonCount = (int)buttonTitles.count;
    NSMutableArray *frames = [NSMutableArray array];
//    NSMutableArray *buttons = [NSMutableArray array];
    for (int i = 0; i < buttonCount; i++) {
        NSString *title = buttonTitles[i];
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button setTag:i];
        [button setBackgroundColor:[UIColor yellowColor]];
//        [button setFrame:CGRectMake(0, 0, 100, 100)];
        button.titleLabel.font = [UIFont systemFontOfSize:buttonTitleFont];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonStackView addArrangedSubview:button];
//        [buttons addObject:button];
        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:buttonTitleFont]}];
        CGFloat buttonWidth = SWIDTH / buttonCount;
        CGFloat indicatorWidth = titleSize.width + 10.f;
        if (indicatorWidth > buttonWidth) {
            indicatorWidth = buttonWidth;
        }
        CGFloat x = buttonWidth * i + (buttonWidth - indicatorWidth) / 2;
        CGRect frame = CGRectMake(x, SHEIGHT - defaultIndicatorHeight, indicatorWidth, defaultIndicatorHeight);
        [frames addObject:[NSValue valueWithCGRect:frame]];
        if (i == 0) {
            button.selected = YES;
            self.indicatorView.frame = frame;
        }
    }
    self.indicatorFrames = frames.copy;
}
- (void)buttonClick:(UIButton *)sender {
    if (self.selectedButton && self.selectedButton.tag == sender.tag) {
        return;
    }
    self.selectedButton.selected = NO;
    sender.selected = YES;
    self.selectedButton = sender;
    NSValue *value = self.indicatorFrames[sender.tag];
    CGRect frame = value.CGRectValue;
    NSLog(@"%@", NSStringFromCGRect(frame));
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.frame = frame;
    }];
    if (self.buttonClick) {
        self.buttonClick(sender.tag);
    }
}

- (void)selectButtonWithIndex:(NSInteger)index {
    if (self.selectedButton && self.selectedButton.tag == index) {
        return;
    }
    for (UIView *view in self.buttonStackView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)view;
            if (btn.tag == index) {
                [self buttonClick:btn];
            }
        }
    }
}

@end
